#include "CHoneycombPickColor.h"

#include <QtMath>

#include <QDebug>
#include <QMouseEvent>

CHoneycombPickColor::CHoneycombPickColor(QWidget *parent) : QWidget(parent)
{
    m_ptClicked = QPoint(0,0);
//    installEventFilter(this);
}

void CHoneycombPickColor::setHexagonRad(int nHexagonRad)
{
    if(nHexagonRad > m_nHexagonMinRad  && nHexagonRad < m_nHexagonMaxRad)
    {
        m_nHexagonRad = nHexagonRad;
        update();
    }
}

void CHoneycombPickColor::setHexagonRadRange(int nMin, int nMax)
{
    m_nHexagonMinRad = nMin;
    m_nHexagonMaxRad = nMax;
}

void CHoneycombPickColor::setHexagonRadStep(int nStep)
{
    m_nHexagonStepRad = nStep;
}

void CHoneycombPickColor::mousePressEvent(QMouseEvent *event)
{
    m_bIsPressed = true;
    m_ptClicked = event->pos();
    //转换点击坐标和绘图坐标一致  窗口中心为原点坐标
    m_ptClicked += QPoint(-width()/2,-height()/2);
    update();
}

void CHoneycombPickColor::mouseMoveEvent(QMouseEvent *event)
{
    m_ptClicked = event->pos();
    //转换点击坐标和绘图坐标一致  窗口中心为原点坐标
    m_ptClicked += QPoint(-width()/2,-height()/2);

    update();
}

void CHoneycombPickColor::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event)
    m_bIsPressed = false;
}

void CHoneycombPickColor::wheelEvent(QWheelEvent *event)
{
    if(event->delta() > 0)
    {
        //放大
        if(m_nHexagonRad < m_nHexagonMaxRad){
            m_nHexagonRad += m_nHexagonStepRad;
            update();
        }
    }
    else
    {
        //缩小
        if(m_nHexagonRad > m_nHexagonMinRad){
            m_nHexagonRad -= m_nHexagonStepRad;
            update();
        }
    }
}

void CHoneycombPickColor::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.translate(width()/2,height()/2);
    painter.setPen(Qt::NoPen);

    //获取最大半径
    int nMaxRad = qMin(width(),height()) / 2;
    //六边形宽
    double nCellWidth = sqrt(3)*m_nHexagonRad; //sqrt(3)/2*m_nHexagonRad*2;  六边形宽度公式: 3的平方根乘六边形外切圆半径


    painter.setBrush(QBrush(Qt::white));
    drawHexagon(&painter,QPoint(0,0),m_nHexagonRad);
    for(int j = 0; j < nMaxRad / nCellWidth;j++){
        QPainterPath path = hexagonPath(nCellWidth*j); //j圈六边形的轮廓
//        painter.drawPath(path);
        for(int i = 0; i < 6*j; i++){
            qreal dRadScale = i/(6.0 * j); //半径比率 0~1.0
            QPointF point = path.pointAtPercent(dRadScale);//获取六边形格子的中心位置
            setHexagonBrush(painter,360.0 * dRadScale, j/(double)((nMaxRad+nCellWidth) / nCellWidth));
            drawHexagon(&painter,point.toPoint(),m_nHexagonRad);
        }
    }
}

void CHoneycombPickColor::drawHexagon(QPainter *painter,QPoint pt, int nRad)
{
    nRad -= 2; //六边形之间的间隙
    painter->save();

    QPainterPath path = hexagonPath(nRad,pt,90);

    //选中六边形格子的边框
    if(path.contains(m_ptClicked)){
        path = hexagonPath(nRad - 2,pt,90); //预留边框空间
        QColor color = painter->brush().color();
        painter->setPen(QPen(color.darker(),3,Qt::DashLine));

        if(m_colorSelected != color){
            m_colorSelected = color;
            emit sigSelected(m_colorSelected);
//            qDebug()<<"选中颜色发生改变:"<<m_colorSelected.name();
//            painter->drawText(0,-height()/2+20,QString("h:%1,s:%2,l:%3")
//                              .arg(color.hslHue())
//                              .arg(color.hslSaturation())
//                              .arg(color.lightness()));
        }
    }

    painter->drawPath(path);
    painter->restore();
}

QPainterPath CHoneycombPickColor::hexagonPath(int nRad, QPoint pt, int nRotate)
{
    //int nRad 六边形外切圆半径
    //QPoint pt 圆心坐标
    //int nRotate 六边形旋转角度

    QPainterPath path;
    for(int i = 0; i <= 6; i++){
        int angle = 60*i + nRotate;  //角度
        float angleArc = (angle % 360) * 3.1415926 / 180; //转换为弧度
        int x = ((float)nRad)*cos(angleArc) + pt.x();
        int y = ((float)nRad)*sin(angleArc) - pt.y();

        if(0 == i){
            path.moveTo(x,y);
        }else{
            path.lineTo(x,y);
        }
    }

    return path;
}

void CHoneycombPickColor::setHexagonBrush(QPainter &painter,int nAngle, qreal dRadScale)
{
    //根据角度 半径比 计算填充颜色
    int h = nAngle;
    int s = 255;
    int l = 255 * (1.0 - dRadScale);
    l = l < 255*0.2 ? 0.2*255 : l;

    QColor color = QColor::fromHsl(h,s,l);
    painter.setBrush(QBrush(color));
}
